<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use app\admin\model\BackupModel;
use \tp5er\Backup as Backups;
use think\Session;

//管理员
class Backup extends Controller
{
    public function index()
    {
		
        return $this->fetch();
    }
    public function lists()
    {
        $page['page']=input('page');
		$where=[];
		if(input('time')){ 
			$times=explode(' , ',input('time'));
			$time_l=strtotime($times[0]);
			$time_s=strtotime($times[1].' 23:59:59');
			$where['backup_time']=['between time',[$time_l,$time_s]];
		}
		// dump($where);
		$BackupModel=new BackupModel;
		$data = $BackupModel->where($where)->group('backup_name')->order('id', 'desc')->paginate(input('limit'),false,$page)->each(function ($item, $key) {
		//     $admin_service     = new AdminService();
			$item['adminname'] = Db::name('admin')->where(array('id'=>$item['backup_admin_id']))->value('adminname');
			$item['backup_time'] = date("Y-m-d H:i",$item['backup_time']);
			return $item;
		});
		//获取分页显示
		$page = $data->render();
		$list = $data->items();
		$lists['code']=0;
		$lists['msg']='';
		$lists['count']=$BackupModel->where($where)->count();
		$lists['data']=$list;
		echo json_encode($lists);
    }
	public function save(){
		$BackupModel=new BackupModel;
		$info=$BackupModel->find(input('id'));
		
		$config=array(
			'path'     => $info['backup_name'],//数据库备份路径
			'part'     => 20971520,//数据库备份卷大小
			'compress' => 0,//数据库备份文件是否启用压缩 0不压缩 1 压缩
			'level'    => 9 //数据库备份文件压缩级别 1普通 4 一般  9最高
		);
		$db= new Backups($config);
		$start=0;
		// dump($info['backup_name'].$info['name'].'-1.sql');
		$file=['name'=>$info['backup_name'],1=>$info['backup_name'].$info['name'].'-1.sql'];
 		$start= $db->setFile($file)->import($start);
        // dump($start);die;
		if($start==0){
			return message('成功');
		}
		return message('失败',false);
	}
  
	public function add(){
		if (request()->isPost()) {
			$date=date('Ymd-His');
			$table='admin';
			if($this->start($table,$date)){
				
				return message('备份成功');
			}
			return message('备份失败',false);
		}
		else {
			return $this->fetch('backup');
	
	   }
	}
	
	public function start($tables,$date){
			$config=array(
				'path'     => './static/database/'.$date.'/',//数据库备份路径
				'part'     => 209715200,//数据库备份卷大小
				'compress' => 0,//数据库备份文件是否启用压缩 0不压缩 1 压缩
				'level'    => 9 //数据库备份文件压缩级别 1普通 4 一般  9最高
			);
			
			$db= new Backups($config);
			$arr=$db->dataList();
			
			foreach($arr as $k=>$v){
				if($v['name']=='backup' || $v['name']=='admin_log'){
					
				}else{
					$file=['name'=>$date,'part'=>1];
					$start= $db->setFile($file)->backup($v['name'], 0);
				}
			}
			$admin=Session::get('admin');
			$data[$k]['backup_admin_id'] = $admin['id'];
			$data[$k]['backup_name'] =$config['path'];
			$data[$k]['name'] =$date;
			$data[$k]['backup_time'] = time();
			// dump($start);die;
			//3.在数据库中生成一份备份目录
			
			//执行
			if ((new BackupModel())->saveAll($data)){
				return true;
			}else{
				return false;
			}
	}
	//修改状态
	public function status(){
		$data['status']=input('sta');
		if(input('sta')=='2'){
			$data['sta']='2';
		}
		if(Db::name('Backup')->where(array('id'=>input('id')))->update($data)){
			return message('成功');
         }
         return message('成功');
	}
	
	public function del_list(){
		$info=BackupModel::get(input('id'));
		// dump($info['backup_name']);die;
		if(BackupModel::destroy(input('id'))){
			file_dels($info['backup_name'].$info['name'].'-1.sql',$info['backup_name']);
			// $config=array(
			// 	'path'     => './static/database/'.$date.'/',//数据库备份路径
			// 	'part'     => 209715200,//数据库备份卷大小
			// 	'compress' => 0,//数据库备份文件是否启用压缩 0不压缩 1 压缩
			// 	'level'    => 9 //数据库备份文件压缩级别 1普通 4 一般  9最高
			// );
			
			return message('成功');
         }
         return message('成功');
	}
	public function delall(){
		$post=input('post.');
		$BackupModel=new BackupModel;
		$w['id']=['in',$post['id']];
		$list=$BackupModel->where($w)->select();
		if(BackupModel::destroy($post['id'])){
			foreach($list as $k=>$v){
				file_dels($v['backup_name'].$v['name'].'-1.sql',$v['backup_name']);
			}
			return message('成功');
         }
		return message('操作失败',false);
	}
  
	//上传图片
	public function upload_img(){
		// dump(config('ImgPath').config('imgprefix'));
		return upload_img(request()->file('file')); // 获取上传的文件);
		
	}
	
}
